<?php
/**
 * Created by PhpStorm.
 * User: dyane
 * Date: 2018/12/17
 * Time: 9:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
use think\Validate;

class Order extends Base
{
    /**
     * 业务分类列表页面
     * @return \think\response\View
     */
    public function cateIndex()
    {
        return view();
    }

    /**
     * 分类列表数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCateList()
    {
        $param = $this->request->param();
        $where = [
            'is_delete'=>0
        ];
        if (!empty($param['cate_name'])) {
            $where['cate_name'] = ['like','%' . $param['cate_name'] .'%'];
        }
        $count = Db::name('order_server_cate')->where($where)->count();

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $tol = ($page - 1) * $pageSize;
        $list = Db::name('order_server_cate')
            ->where($where)
            ->limit($tol,$pageSize)
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
            $list[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
        }
        //返回数据
        return ["code" => "0", "msg" => "", "count" => $count, "data" => $list];
    }

    /**
     * 添加 业务分类数据页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cateAddPage()
    {
        $param = $this->request->param();
        if (!empty($param['id'])) {
            $info = Db::name('order_server_cate')->where(['id' => $param['id']])->find();
            $this->assign('info', $info);
        }
        return view();
    }

    /**
     * 添加 业务分类数据
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function cateAdd()
    {
        $param = $this->request->param();
        $rule = [
            'cate_name|分类名'=>'require',
            'img|分类图片'=>'require',
            'code|分类简称'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code'=>400,'msg'=>$validate->getError()]);die;
        }
        if(!empty($param['id'])){
            $info = Db::name('order_server_cate')->field('img')->where(['id'=>$param['id']])->find();
            if(!empty($info['img'])){
                if($info['img'] != $param['img']){
                    $url = ROOT_PATH . 'public' . $info['img'];
                    if(file_exists($url)){
                        unlink($url);
                    }
                }
            }
            $data = [
                'update_time'=>time(),
                'cate_name'=>$param['cate_name'],
                'code'=>$param['code'],
                'img'=>$param['img']
            ];
            $res = Db::name('order_server_cate')->where(['id'=>$param['id']])->update($data);
        }else{
            $data = [
                'cate_name'=>$param['cate_name'],
                'add_time'=>time(),
                'update_time'=>time(),
                'img'=>$param['img']
            ];
            $res = Db::name('order_server_cate')->insert($data);
        }
        if($res){
            echo json_encode(['code'=>200,'msg'=>'操作成功']);die;
        }else{
            echo json_encode(['code'=>400,'msg'=>'操作失败']);die;
        }
    }


    /**
     * 删除分类数据
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delCate()
    {
        $param = $this->request->param();
        $res = Db::name('order_server_cate')->where(['id'=>$param['id']])->update(['is_delete'=>1]);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>400,'msg'=>'删除失败']);
        }
    }



        /********************************一级**********************************************/
    public function classIndex()
    {
        return view();
    }

    /**
     * 分类列表数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getClassList()
    {
        $param = $this->request->param();
        $where = [
            'is_delete'=>0
        ];
        if (!empty($param['cate_name'])) {
            $where['cate_class'] = ['like','%' . $param['cate_name'] .'%'];
        }
        $count = Db::name('system_cate_class')->where($where)->count();

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $tol = ($page - 1) * $pageSize;
        $list = Db::name('system_cate_class')
            ->where($where)
            ->limit($tol,$pageSize)
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
            $list[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
        }
        //返回数据
        return ["code" => "0", "msg" => "", "count" => $count, "data" => $list];
    }

    public function cateAddPage1()
    {
        $param = $this->request->param();
        if(isset($param['id']) && !empty($param['id'])){
            $info = Db::table('system_cate_class')->where('id',$param['id'])->find();
            $this->assign('info',$info);
        }
        $cate = Db::table('order_server_cate')->where('is_delete',0)->select();
        $this->assign('cate',$cate);
        return view();
    }

    public function cateAdd1()
    {
        $param = $this->request->param();
        $rule = [
            'cate_id|分类名称'=>'require',
            'cate_class|业务分类'=>'require'
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code'=>400,'msg'=>$validate->getError()]);
        }
        $data = [
            'cate_class'=>$param['cate_class'],
            'cate_id'=>$param['cate_id']
        ];

        if(!empty($param['id'])){
            $data['update_time'] = time();
            $res = Db::table('system_cate_class')->where('id',$param['id'])->update($data);
        }else{
            $res = Db::table('system_cate_class')->insert($data);
        }
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }

    public function delCate1()
    {
        $param = $this->request->param();
        $info = Db::table('system_cate_resource')->where('cate_class_id',$param['id'])->find();
        if($info){
            return json(['code'=>400,'msg'=>'该编码下有二级编码,不得删除']);
        }
        $res = Db::name('system_cate_class')->where(['id'=>$param['id']])->update(['is_delete'=>1]);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>400,'msg'=>'删除失败']);
        }
    }

    /******************************二级*******************************************/
    public function resourceIndex()
    {
        return view();
    }

    /**
     * 分类列表数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getResourceList()
    {
        $param = $this->request->param();
        $where = [
            'system_cate_resource.is_delete'=>0
        ];
        if (!empty($param['cate_name'])) {
            $where['system_cate_resource.resource'] = ['like','%' . $param['cate_name'] .'%'];
        }
        $count = Db::name('system_cate_resource')->where($where)->count();

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $tol = ($page - 1) * $pageSize;
        $list = Db::name('system_cate_resource')
            ->join('system_cate_class','system_cate_resource.cate_class_id=system_cate_class.id','left')
            ->where($where)
            ->field('system_cate_class.cate_class,system_cate_resource.*')
            ->limit($tol,$pageSize)
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
            $list[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
        }
        //返回数据
        return ["code" => "0", "msg" => "", "count" => $count, "data" => $list];
    }

    public function cateAddPage2()
    {
        $param = $this->request->param();
        if(isset($param['id']) && !empty($param['id'])){
            $info = Db::table('system_cate_resource')->where('id',$param['id'])->find();
            $this->assign('info',$info);
        }
        $cate = Db::table('system_cate_class')->where('is_delete',0)->select();
        $this->assign('cate',$cate);
        return view();
    }

    public function cateAdd2()
    {
        $param = $this->request->param();
        $rule = [
            'cate_class_id|一级分类'=>'require',
            'resource|二级分类'=>'require'
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code'=>400,'msg'=>$validate->getError()]);
        }

        $data = [
            'cate_class_id'=>$param['cate_class_id'],
            'resource'=>$param['resource']
        ];

        if(!empty($param['id'])){
            $data['update_time'] = time();
            $res = Db::table('system_cate_resource')->where('id',$param['id'])->update($data);
        }else{
            $data['add_time'] = time();
            $res = Db::table('system_cate_resource')->insert($data);
        }
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }




    public function delCate2()
    {
        $param = $this->request->param();
        $info = Db::table('system_code')->where('resource_id',$param['id'])->find();
        if($info){
            return json(['code'=>400,'msg'=>'该编码下有三级编码,不得删除']);
        }
        $res = Db::name('system_cate_resource')->where(['id'=>$param['id']])->update(['is_delete'=>1]);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>400,'msg'=>'删除失败']);
        }
    }

    /******************************三级*******************************************/
    /**
     * 业务代码数据页面
     * @return \think\response\View
     */
    public function codeIndex()
    {
        $class = Db::name('system_cate_class')->field('id,cate_class')->order('id desc')->select();
        $this->assign('class',$class);
        return view();
    }

    /**
     * 获取业务代码数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCode()
    {
        $param = $this->request->param();
        $where = '1 = 1 and c.is_delete = 0';
        if (!empty($param['code'])) {
            $where .= ' and c.code like "%' . $param['code'] .'%"';
        }
        if(!empty($param['class_id'])){
            $where .= ' and c.class_id = '.$param['class_id'];
        }
        $count = Db::name('system_code')->where($where)->alias('c')->count();
        $limit = Request::instance()->param('limit')?Request::instance()->param('limit'):10;
        $page = Request::instance()->param('page')?Request::instance()->param('page'):1;
        $tol = ($page - 1) * $limit;
        $list = Db::name('system_code')
            ->field('c.id,c.code,c.add_time,c.update_time,osc.cate_name,scc.cate_class,c.denger_thing,c.denger_identity,scr.resource')
            ->alias('c')
            ->where($where)
            ->join('system_cate_resource scr','scr.id = c.resource_id')
            ->join('system_cate_class scc','scc.id = scr.cate_class_id')
            ->join('order_server_cate osc','osc.id = scc.cate_id')
            ->limit($tol,$limit)
            ->order('c.id desc')
            ->select();
        foreach ($list as $key => $value){
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
            $list[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
        }
        //返回数据
        return ["code" => "0", "msg" => "", "count" => $count, "data" => $list];
    }

    /**
     * 修改|添加 业务代码页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function codeModifyPage()
    {
        $param = $this->request->param();
        if(!empty($param['id'])){
            $info = Db::name('system_code')->field('id,code,class_id,denger_identity,denger_thing')->where(['id'=>$param['id']])->find();
            $this->assign('info',$info);
        }
        $class = Db::name('system_cate_resource')
            ->alias('scr')
            ->field('scr.id,scr.resource,scc.cate_class')
            ->join('system_cate_class scc','scc.id = scr.cate_class_id')
            ->order('scr.id desc')
            ->select();
        $this->assign('class',$class);
        return view();
    }

    /**
     * 修改|添加 业务代码
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function codeModify()
    {
        $param = $this->request->param();
        $rule = [
            'code|业务代码'=>'require',
            'resource_id|所属分类'=>'require',
            'denger_identity|危险特性'=>'require',
            'denger_thing|危险废物'=>'require'
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code'=>400,'msg'=>$validate->getError()]);die;
        }
        $data = [
            'code'=>$param['code'],
            'resource_id'=>$param['resource_id'],
            'update_time'=>time(),
            'denger_thing'=>$param['denger_thing'],
            'denger_identity'=>$param['denger_identity']
        ];
        if(!empty($param['id'])){
            $res = Db::name('system_code')->where(['id'=>$param['id']])->update($data);
        }else{
            $data = [
                'add_time'=>time(),
            ];
            $res = Db::name('system_code')->insert($data);
        }
        if($res){
            echo json_encode(['code'=>200,'msg'=>'操作成功']);die;
        }else{
            echo json_encode(['code'=>400,'msg'=>'操作失败']);die;
        }
    }

    /**
     * 删除业务代码
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delCode()
    {
        $param = $this->request->param();
        $res = Db::name('system_code')->where(['id'=>$param['id']])->update(['is_delete'=>1]);
        if($res){
            echo json_encode(['code'=>200,'msg'=>'删除成功']);die;
        }else{
            echo json_encode(['code'=>400,'msg'=>'删除失败']);die;
        }
    }



    /******************************结束*******************************************/





    /**
     * 添加行业来源页面
     * @return \think\response\View
     */
    public function resourceAddPage()
    {
        $cate = Db::name('system_cate_class')
            ->alias('scc')
            ->field('scc.id,scc.cate_class,osc.cate_name')
            ->where(['scc.is_delete'=>0])
            ->join('order_server_cate osc','osc.id = scc.cate_id')
            ->order('scc.id desc')
            ->select();
        $this->assign('cate',$cate);
        return view();
    }
    public function resourceAdd()
    {
        $param = $this->request->param();
        $rule = [
            'cate_class_id|分类级别'=>'require',
            'resource|行业来源'=>'require'
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code'=>400,'msg'=>$validate->getError()]);die;
        }
        $data = [
            'resource'=>$param['resource'],
            'add_time'=>time(),
            'update_time'=>time(),
            'cate_class_id'=>$param['cate_class_id']
        ];
        $res = Db::name('system_cate_resource')->insert($data);
        if($res){
            echo json_encode(['code'=>200,'msg'=>'操作成功']);die;
        }else{
            echo json_encode(['code'=>400,'msg'=>'操作失败']);die;
        }
    }



    /**
     * 添加业务级别页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function classAddPage()
    {
        $cate = Db::name('order_server_cate')->field('id,cate_name')->where(['is_delete'=>0])->order('id desc')->select();
        $this->assign('cate',$cate);
        return view();
    }

    /**
     * 添加业务分类级别数据
     */
    public function classAdd()
    {
        $param = $this->request->param();
        $rule = [
            'cate_id|分类名'=>'require',
            'cate_class|业务分类'=>'require'
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code'=>400,'msg'=>$validate->getError()]);die;
        }
        $data = [
            'cate_id'=>$param['cate_id'],
            'cate_class'=>$param['cate_class'],
            'add_time'=>time(),
            'update_time'=>time()
        ];
        $res = Db::name('system_cate_class')->insert($data);
        if($res){
            echo json_encode(['code'=>200,'msg'=>'添加成功']);die;
        }else{
            echo json_encode(['code'=>400,'msg'=>'添加失败']);die;
        }
    }


    /**
     *
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function modifyPage()
    {
        $param = $this->request->param();
        $cate = Db::name('order_server_cate')->field('id,cate_name')->order('id desc')->select();
        $info = Db::name('system_cate_class')->field('id,cate_class,cate_id')->where(['id'=>$param['id']])->find();
        $this->assign('cate',$cate);
        $this->assign('info',$info);
        return view();
    }

//    /**
//     * 修改分类级别数据
//     * @throws \think\Exception
//     * @throws \think\exception\PDOException
//     */
//    public function classModify()
//    {
//        $param = $this->request->param();
//        $data = [
//            'cate_id'=>$param['cate_id'],
//            'cate_class'=>$param['cate_class'],
//            'update_time'=>time()
//        ];
//        $res = Db::name('system_cate_class')->where(['id'=>$param['id']])->update($data);
//        if($res){
//            echo json_encode(['code'=>200,'msg'=>'修改成功']);die;
//        }else{
//            echo json_encode(['code'=>400,'msg'=>'修改失败']);die;
//        }
//    }





}